package com.wl.servlets;

import com.wl.fruit.dao.FruitDAO;
import com.wl.fruit.dao.impl.FruitDAOImpl;
import com.wl.fruit.pojo.Fruit;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/update.do")
public class UpdateServlet extends ViewBaseServlet{
    private FruitDAO fruitDAO = new FruitDAOImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 防止中文乱码
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        Integer fid = Integer.parseInt(req.getParameter("fid"));
        String fname = req.getParameter("fname");
        Integer price = Integer.parseInt(req.getParameter("price"));
        Integer fcount = Integer.parseInt(req.getParameter("fcount"));
        String remark = req.getParameter("remark");
        boolean flag = fruitDAO.updateFruit(new Fruit(fid, fname, price, fcount, remark));
//        super.processTemplate("index", req, resp);
        // 重定向 目的是重新给indexServlet发请求 重新获取fruitList,然后覆盖到session中 这样index.html页面显示的session中数据才是最新的
        resp.sendRedirect("index");
    }
}
